﻿using ControlzEx.Standard;
using MahApps.Metro.Controls.Dialogs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;

namespace ScriptManager
{
    /// <summary>
    /// cdkRes.xaml 的交互逻辑
    /// </summary>
    public partial class cdkRes
    {
        public string userKey;
        public int polat = 0;
        MainWindow mw;
        public cdkRes(MainWindow mainWindow)
        {
            InitializeComponent();
            mw = mainWindow;
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //兑换CDK
            WebRequest req = WebRequest.Create($"http://warframe.md4.cn:8081/warframeservice/warframeluaSerivce/registerCDK?userKey={userKey}&CDK={cdkInput.Text}");
            WebResponse resp = req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
            string strResponse = sr.ReadToEnd();
            if (strResponse.Equals("-1"))
            {
                //this.ShowMessageAsync("","无效的CDK");
                MessageBox.Show("无效的CDK");
            }
            else
            {
                MessageBox.Show($"兑换成功，点数增加{strResponse}");
                mw.refershPolat();
                //this.ShowMessageAsync("", $"兑换成功，点数增加{strResponse}");
            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //判断是否为正整数
            try {
                int platAdd = int.Parse(tianshu.Text);
                if (platAdd < 1)
                {
                    throw new Exception();
                }
                else {
                    //兑换天数
                    WebRequest req = WebRequest.Create($"http://warframe.md4.cn:8081/warframeservice/warframeluaSerivce/exchangePloat?userKey={userKey}&ploat={platAdd}&type=2");
                    WebResponse resp = req.GetResponse();
                    StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
                    string strResponse = sr.ReadToEnd();
                    if (strResponse.Equals("1"))
                    {
                        this.ShowMessageAsync("","兑换成功");
                        mw.refershPolat();
                        mw.getrevkDate();
                    }
                }
            }
            catch (Exception)
            {
                this.ShowMessageAsync("","输入的天数非法");
            }

        }
    }
}
